﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Croxweb.Leuphana.ViewModels;

namespace Croxweb.Leuphana.Pages
{
    public partial class SchedulePage : PhoneApplicationPage
    {
        private ScheduleViewModel viewModel;

        public SchedulePage()
        {
            InitializeComponent();

            // Datacontext to view model.
            viewModel = new ScheduleViewModel(App.MyStudyService);
            DataContext = viewModel;
        }

        // Load view model data.
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (!viewModel.IsDataLoaded)
            {
                viewModel.Refresh();
            }

            base.OnNavigatedTo(e);
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            if (listBox != null)
            {
                AppointmentViewModel avm = listBox.SelectedItem as AppointmentViewModel;
                if (avm != null)
                {
                    NavigationService.Navigate(new Uri("/Pages/LecturePage.xaml?id=" + avm.LectureId, UriKind.Relative));
                    listBox.SelectedIndex = -1;
                }
            }
        }

        private void Refresh_Click(object sender, EventArgs e)
        {
            viewModel.SyncSchedule();
        }
    }
}